package com.qtummatrix.controller;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author jsl
 * @date 2019/11/25 - 9:48
 */
@Controller("FileUpload")
public class FileUpload extends ActionSupport {
    //上传保存路径
    private String savePath;
    //文件上传前的原始路径名称
    private File myFile;
    //获取到上传的文件名称
    private String myFileFileName;
    private Map<String, Object> info = new HashMap<String, Object>();

    public Map<String, Object> getInfo() {
        return info;
    }

    public void setInfo(Map<String, Object> info) {
        this.info = info;
    }

    public String getSavePath() {
        return savePath;
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }

    public File getMyFile() {
        return myFile;
    }

    public void setMyFile(File myFile) {
        this.myFile = myFile;
    }

    public String getMyFileFileName() {
        return myFileFileName;
    }

    public void setMyFileFileName(String myFileFileName) {
        this.myFileFileName = myFileFileName;
    }

  /*  public String upload() {
        //获取上传文件再服务器中的物理路径
        String Path = ServletActionContext.getServletContext().getRealPath(savePath);
        //上传 源路径
        try {
            FileUtils.copyFile(myFile, new File(Path + "\\" + myFileFileName));
        } catch (IOException e) {
            e.printStackTrace();
            return INPUT;
        }
        System.out.println(Path);
        return SUCCESS;
    }*/

    public String jsonUpload() {
        //获取上传文件再服务器中的物理路径
        String Path = ServletActionContext.getServletContext().getRealPath(savePath);
        //上传 源路径
        try {
            FileUtils.copyFile(myFile, new File(Path + "\\" + myFileFileName));
            savePath = savePath.substring(1);
            info.clear();
            info.put("Path", savePath + "\\" + myFileFileName);
            info.put("message", "上传成功");
        } catch (IOException e) {
            e.printStackTrace();
            info.put("message", "上传失败");
            return INPUT;
        }
        System.out.println(Path);
        return SUCCESS;
    }
}
